package BinarySearchTree;

public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root = null;
    /*
    * 查找二叉搜索树中指定的val值
    * */
    public TreeNode find(int val) {
        TreeNode cur = root;
        if (root == null) {
            return null;
        }
        while (cur != null) {
            if (val < cur.val) {
                cur = cur.left;
            } else if (val > cur.val) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }
    //插入值
    public void insert(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (val < cur.val) {
                parent = cur;
                cur = cur.left;
            }else if (val > cur.val) {
                parent = cur;
                cur = cur.right;
            }else {
                return;
            }
        }
        //cur空了
        TreeNode node = new TreeNode(val);
        if (parent.val < val) {
            parent.right = node;
        }else {
            parent.left = node;
        }
    }

    //中序遍历
    public TreeNode inorder(TreeNode root) {
        if (root == null) {
            return root;
        }
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
        return root;
    }
    //删除二叉搜索树中的指定的val值
    public void remove(int val) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (val == cur.val) {
                removeNode(parent,cur);
                return;
            }else if (val < cur.val) {
                parent = cur;
                cur = cur.left;
            }else {
                parent = cur;
                cur = cur.right;
            }
        }
    }
    //替换法，考虑多种情况
    public void removeNode(TreeNode parent,TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            }else if (cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            }else if (parent.left == cur) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {//两边都不是空的  用替换法删除
            //在cur的右树的最左边找一个最小值
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            //target左边空了   也有可能target没有左树  反正找到了target
            cur.val = target.val;
            //删除这个最小值即可  但是要注意可能没有左树，所以分两种情况
            if (target == targetParent.left) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }

    public  TreeNode head = null;
    public TreeNode pre = null;
    public TreeNode convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null) return null;
        convert(pRootOfTree.left);
        if (pre == null) {
            head = pRootOfTree;
            pre = pRootOfTree;
        }else {
            pre.right = pRootOfTree;
            pRootOfTree.left = pre;
            pre = pRootOfTree;
        }
        return head;
    }
}
